package com.tiancheng.trade.commom.mq.rabbit.autoconfigure;

import com.tiancheng.trade.commom.mq.rabbit.config.MyRabbitProperties;
import com.tiancheng.trade.commom.mq.rabbit.config.RabbitModuleInitializer;
import com.tiancheng.trade.commom.mq.rabbit.producer.RabbitProducerManager;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: kellen
 * @Description: RabbitMQ 自动装配
 * @Date: create in 2024/11/26 15:29
 */
@Configuration
@ConditionalOnProperty(name = "mq.type", havingValue = "rabbit")
@ConditionalOnBean(MyRabbitProperties.class)
public class RabbitMQAutoConfiguration {
    /**
     * 通过yaml配置，创建队列、交换机初始化器
     */
    @Bean
    @ConditionalOnMissingBean
    public RabbitModuleInitializer rabbitModuleInitializer(AmqpAdmin amqpAdmin, MyRabbitProperties rabbitProperties) {
        return new RabbitModuleInitializer(amqpAdmin, rabbitProperties);
    }


    @Bean
    @ConditionalOnMissingBean
    public RabbitProducerManager rabbitProducerManager(final AmqpTemplate amqpTemplate){
        return new RabbitProducerManager(amqpTemplate);
    }
}
